package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.service.AddressService;
import com.itheima.service.SetMealService;
import com.itheima.service.UserService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 自定义job  使用redis对图片服务器和数据库进行对比 实现自动清理冗余图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetMealService setMealService;
    @Reference
    private UserService userService;
    @Reference
    private AddressService addressService;


    public void clearImg(){
        System.out.println("正在检查冗余图片..." + new Date());
        Jedis resource = jedisPool.getResource();
        //获取套餐图片数据库真实图片集合
        List<String> list = setMealService.getAllImg();
        List<String> list1 = userService.findAllImg();
        List<String> list2 = addressService.getAllImg();
        //删除原数据
        resource.del(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        resource.del(RedisConstant.ADDRESS_PIC_DB_RESOURCES);
        //同步到redis集合
        for (String s : list) {
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,s);
        }
        for (String s : list1) {
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,s);
        }
        for (String s : list2) {
            resource.sadd(RedisConstant.ADDRESS_PIC_DB_RESOURCES,s);
        }

        //根据redis保存的两个set集合 进行差别计算 获取冗余图片集合     比较多的数据在前, 较少的在后
        Set<String> set = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set.size() > 0 && set != null){
            System.out.println("找到的冗余图片: " + set);
            for (String imgFileName : set) {
                //先删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(imgFileName);
                //删除redis集合中的图片记录
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgFileName);
                System.out.println("已删除图片: " + imgFileName);
            }
        }else{
            System.out.println("未找到冗余图片!");
        }

        //根据redis保存的两个set集合 进行差别计算 获取冗余图片集合     比较多的数据在前, 较少的在后
        Set<String> set2 = resource.sdiff(RedisConstant.ADDRESS_PIC_RESOURCES, RedisConstant.ADDRESS_PIC_DB_RESOURCES);
        if (set2.size() > 0 && set2 != null){
            System.out.println("找到的冗余图片: " + set2);
            for (String imgFileName : set2) {
                //先删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(imgFileName);
                //删除redis集合中的图片记录
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgFileName);
                System.out.println("已删除图片: " + imgFileName);
            }
        }else{
            System.out.println("未找到冗余图片!");
        }
        //释放资源
        resource.close();
    }

}
